using CommunityToolkit.Mvvm.ComponentModel;
using SimpleToDoList.Models;
using SimpleToDoList.Services;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace SimpleToDoList.ViewModels
{
    /// <summary>
    /// 加载视图 可用于做登录界面或者加载数据
    /// </summary>
    public partial class LoadViewViewModel : ViewModelBase
    {
        public LoadViewViewModel()
        {
            UpdateProcess();
        }

        public Action<List<ToDoItem>>? ToMainAction { get; set; }

        [ObservableProperty]
        private double _progress;

        private async void UpdateProcess()
        {
            // 模拟加载的过程
            for (var i = 0; i < 80; i++)
            {
                Progress = i;
                await Task.Delay(10); // 模拟加载
            }

            var itemsLoaded = await ToDoListFileService.ReadFileAsync() ?? new List<ToDoItem>();

            for (var i = 80; i <= 100; i++)
            {
                Progress = i;
                await Task.Delay(10); // 模拟加载
            }

            ToMainAction?.Invoke(itemsLoaded);
        }
    }
}